package com.xsmart.oa.services;

import com.xsmart.common.constant.ResultConstant;
import com.xsmart.common.entity.RequestBean;
import com.xsmart.common.entity.ResponseBean;
import com.xsmart.oa.dao.DistributorDao;
import org.apache.commons.collections.MapUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分销商相关服务
 *  测试gitxxxxxxxxcccccccccccccccccccccccccc
 * @author guojd
 */
@Service
public class DistributorService {
    @Autowired
    DistributorDao distributorDao;

    /**
     * 会员绑定分销商
     *
     * @param requestBean 请求对象
     *                    uid:用户id
     *                    duid:分销商用户id
     * @return 绑定结果
     * duid:分销商id
     * res_status：绑定结果
     * rel_msg：绑定信息
     */
    public ResponseBean distributorMemberRel(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        Integer uid = requestBean.getRequestParamInteger("uid");
        Integer duid = requestBean.getRequestParamInteger("duid");
        Integer shareId = requestBean.getRequestParamInteger("shareId");
        if (uid == null) {
            responseBean.setResult(ResultConstant.ERROR);
            responseBean.setMessage("请求错误");
            return responseBean;
        }

        if (shareId != -1){
            Map<String, Object> shareRegLog = new HashMap<>();
            shareRegLog.put("shareId", shareId);
            shareRegLog.put("regUserId", uid);
            distributorDao.addShareRegLog(shareRegLog);
        }

        if (duid == null) {
            responseBean.addResultItem("duid", null);
            responseBean.addResultItem("sellerInfo", null);
            responseBean.addResultItem("res_status", 0);
            responseBean.addResultItem("rel_msg", "无需绑定");
        } else {
            //判断是否已关联分销商
            Map<String, Object> distributorInfo = distributorDao.queryDistributorByUid(uid);
            if (distributorInfo != null && distributorInfo.containsKey("d_user_id")) {
                int sduid = MapUtils.getIntValue(distributorInfo, "d_user_id");
                responseBean.addResultItem("duid", sduid);
                //判断是否为新分销商分享已分销用户
                if (duid != -1 && sduid == duid) {
                    responseBean.addResultItem("res_status", 0);
                    responseBean.addResultItem("rel_msg", "该会员已绑定分销商，不能重新绑定");
                }
                duid = sduid;
            } else {
                if(duid != -1){
                    List<Integer> userType = distributorDao.queryUserType(duid);
                    if (userType.get(0) != null){
                        //当分享用户不是分销商时
                        if (userType.get(0) == 0){
                            Map<String, Object> duserIdMap = distributorDao.queryDistributorByUid(duid);
                            duid = (Integer) duserIdMap.get("d_user_id");
                        }
                    }
                }
                distributorDao.relDistributorAndMember(uid, duid);
                responseBean.addResultItem("duid", duid);
                responseBean.addResultItem("res_status", 1);
            }
            Map<String, Object> sellerInfo = null;
            if (duid != -1) {
                sellerInfo = distributorDao.queryDistributorSellerByDuid(duid);
            }
            responseBean.addResultItem("sellerInfo", sellerInfo);
        }
        responseBean.setResult(ResultConstant.SUCCESS);
        return responseBean;
    }

    /**
     * 会员分享记录
     * @param requestBean 请求对象
     * @return 分享保存结果
     */
    public ResponseBean shareLog(RequestBean requestBean) {
        ResponseBean responseBean = requestBean.getResponseBean();
        int uid=requestBean.getRequestParamInteger("uid");
        int shareId=requestBean.getRequestParamInteger("shareId");
        Map<String, Object> shareLog = new HashMap<>();
        shareLog.put("uid", uid);
        shareLog.put("shareId", shareId);
        distributorDao.addShareLog(shareLog);
        return responseBean;
    }

}
